package com.itheima.bos.base.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.base.service.AreaService;
import com.itheima.bos.domain.Area;
import com.itheima.bos.utils.PinYin4jUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class AreaAction extends ActionSupport implements ModelDriven<Area>{
	private File file;
	
	
	public void setFile(File file) {
		this.file = file;
	}
     
	@Autowired
	private AreaService areaService;
	
	private Area area = new Area();
	@Override
	public Area getModel() {
		
		return area;
	}
	
	@Action(value="area_batchImport")
	public String batchImport() throws IOException{
		
		//基于.xls格式解析HSSF
		//加载excle文件对象
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
		//读取一个sheet
		HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
		//创建一个存放area的集合
		List<Area>  areas = new ArrayList<>();
		
		//读取每一行
		for (Row row : hssfSheet) {
			if(row.getRowNum()==0){
				continue;//第一行跳过
			}
			if(row.getCell(0)==null||StringUtils.isBlank(row.getCell(0).getStringCellValue())){
				continue;//最后一行无数据跳出
			}
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());//1列
			area.setProvince(row.getCell(1).getStringCellValue());//2列
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			// 基于pinyin4j生成城市编码和简码
			String province = area.getProvince();
			String city = area.getCity();
			String district = area.getDistrict();
			
			//截取掉最后的省市区
			province = province.substring(0, province.length() - 1);
			city = city.substring(0, city.length() - 1);
			district = district.substring(0, district.length() - 1);
			
			//转换大写
			String[] headArray =PinYin4jUtils.getHeadByString(province+city+district);
			System.out.println(headArray);
			System.out.println("--------------------------------------");
			StringBuffer buffer = new StringBuffer();
			for (String headStr : headArray) {
				buffer.append(headStr);
			}
			//
			String shortcode = buffer.toString();
			System.out.println(shortcode);
			System.out.println("--------------------------------------");
			//添加简码
			area.setShortcode(shortcode);
			// 城市编码
			String citycode = PinYin4jUtils.hanziToPinyin(city, "");
			area.setCitycode(citycode);

			areas.add(area);
		}
		//调用业务层
		areaService.batchSave(areas);
		return NONE;
	}
	
	
	private int page;
	private int rows;
	
	

	// 分页查询
	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value = "area_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 构造分页查询对象
		Pageable pageable = new PageRequest(page - 1, rows);
		// 构造条件查询对象
		Specification<Area> specification = new Specification<Area>() {
			@Override
			public Predicate toPredicate(Root<Area> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if (StringUtils.isNotBlank(area.getProvince())) {
					Predicate p1 = cb.like(root.get("province")
							.as(String.class), "%" + area.getProvince() + "%");
					list.add(p1);
				}
				if (StringUtils.isNotBlank(area.getCity())) {
					Predicate p2 = cb.like(root.get("city").as(String.class),
							"%" + area.getCity() + "%");
					list.add(p2);
				}
				if (StringUtils.isNotBlank(area.getDistrict())) {
					Predicate p3 = cb.like(root.get("district")
							.as(String.class), "%" + area.getDistrict() + "%");
					list.add(p3);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		// 调用业务层完成查询
		Page<Area> pageData = areaService.findPageData(specification, pageable);
		// 压入值栈
		ActionContext.getContext().getValueStack().push(pageData);

		return SUCCESS;
	}

}
